package gs.gui;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class NPCStatPanel extends JPanel {

	AdminWindow parent;
	private ConnectionController connection;
	DefaultListModel NPCModel;
	JList NPCList;
	JScrollPane NPCScrollPane;
	JTextField nameField;
	JTextField healthField;
	JTextField levelField;
	
	NPCStatPanel(ConnectionController c, AdminWindow win)
	{
		super();
		connection = c;
		parent = win;
		
		this.setLayout(new GridLayout(1,2));
		
		JPanel choicePane = new JPanel(new BorderLayout());
		JPanel statPane = new JPanel(new GridLayout(9,2));
		
		choicePane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Wyb�r"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		statPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Statystyki"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		NPCModel = new DefaultListModel();
		
		NPCList = new JList(NPCModel);
		NPCList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        NPCList.setSelectedIndex(0);
        NPCList.setVisibleRowCount(3);
        NPCScrollPane = new JScrollPane(NPCList);
        
        MouseListener accListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
					getNPCData();
				}
		};
		NPCList.addMouseListener(accListener);
        
        nameField = new JTextField(" ");
        nameField.setEditable(false);
        healthField = new JTextField(" ");
        healthField.setEditable(false);
        levelField = new JTextField(" ");
        levelField.setEditable(false);
        
        getNPCList();
        
        choicePane.add(NPCScrollPane, BorderLayout.CENTER);
        statPane.add(new JLabel("Nazwa:"));
        statPane.add(nameField);
        statPane.add(new JLabel("Zdrowie:"));
        statPane.add(healthField);
        statPane.add(new JLabel("Poziom:"));
        statPane.add(levelField);
        
        add(choicePane);
        add(statPane);
	}
	
	void getNPCData()
	{
		ArrayList<String> a;
		String name = (String) NPCList.getSelectedValue();
		a = connection.getNPCData(name);
		nameField.setText(a.get(0));
		healthField.setText(a.get(1));
		levelField.setText(a.get(2));
	}
	
	void getNPCList()
	{
		ArrayList<String> a;
		
 		a = connection.getNPCList();
		NPCModel.clear();
		for(String s : a)
		{
			NPCModel.addElement(s);
		}
	}


	public void refresh() {
		getNPCList();
	}

}
